package com.crack.spansframe;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.SystemClock;
import android.text.Layout;
import android.text.SpannableStringBuilder;
import android.text.style.LeadingMarginSpan;
import android.text.style.LineBackgroundSpan;
import android.widget.TextView;

import com.crack.spansframe.extra.Clickable;
import com.crack.spansframe.extra.INeedTextView;

/**
 * Created by archermind on 16-4-19.
 */
public class CollSpan implements LineBackgroundSpan, LeadingMarginSpan, Clickable, INeedTextView {

    CharSequence content;
    SpannableStringBuilder sp;
    TextView textView;
    boolean isExpand = false;
    public static final int GAP_WIDTH = 10;
    public static final int ICON_SIZE = 30;

    public CollSpan(SpannableStringBuilder sp, CharSequence content) {
        this.content = content;
        this.sp = sp;
    }

    @Override
    public void drawBackground(Canvas c, Paint p, int left, int right, int top, int baseline, int bottom, CharSequence text, int start, int end, int lnum) {
        if (sp.getSpanStart(this) == start) {
            Layout layout = textView.getLayout();
            end = sp.getSpanEnd(this);
            int endLine = layout.getLineForOffset(end);
            bottom = layout.getLineBottom(endLine);
            int oldClor = p.getColor();
            int lineColor = Color.parseColor("#E0C19E");
            p.setColor(lineColor);
            c.drawLine(left, top, right, top, p);
            c.drawLine(left, bottom, right, bottom, p);
            p.setColor(oldClor);
        }
    }

    @Override
    public void onClick(TextView textView) {
        long s = SystemClock.currentThreadTimeMillis();
        int start = sp.getSpanStart(this);
        int end = sp.getSpanEnd(this);
        Object[] spans = sp.getSpans(start, end, Object.class);
        for (Object span : spans) {
            if (sp.getSpanStart(span) >= start && sp.getSpanEnd(span) <= end && span != this) {
                sp.removeSpan(span);
            }
        }
        sp.replace(start, end, content);
        textView.setText(sp);
        isExpand = true;
//            Log.i("crack", "find-time==>>" + (SystemClock.currentThreadTimeMillis() - s));
    }

    @Override
    public void setTextView(TextView textView) {
        this.textView = textView;
    }

    int height;
    int width;

    @Override
    public int getLeadingMargin(boolean first) {
        if (isExpand) {
            return 0;
        }
        Layout layout = textView.getLayout();
        if (layout == null) {
            return 0;
        }
        int start = sp.getSpanStart(this);
        int line = layout.getLineForOffset(start);
        height = layout.getLineBottom(line) - layout.getLineTop(line);
        width = (int) (height * SpanSolve.sCollRatio);
        return GAP_WIDTH * 2 + width;
    }

    @Override
    public void drawLeadingMargin(Canvas c, Paint p, int x, int dir, int top, int baseline, int bottom, CharSequence text, int start, int end, boolean first, Layout layout) {
        if (isExpand) {
            return;
        }
        Drawable drawable = SpanSolve.sCollDrawable;
        drawable.setBounds(0, 0, width, height);
        Rect rect = drawable.getBounds();
        c.save();
        c.translate(GAP_WIDTH + x, (bottom + top) / 2.0f - rect.bottom / 2.0f);
        drawable.draw(c);
        c.restore();
    }
}
